package codelib.s2sh.web.action;

import codelib.s2sh.biz.IServiceConstant;
import codelib.s2sh.biz.IUserService;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport implements IServiceConstant {

	private static final long serialVersionUID = 1L;

	// 登录的用户信息
	private String userId;
	private String passwd;

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}

	// 依赖的业务逻辑组件
	private IUserService userService;

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public String execute() throws Exception {
		// 创建ActionContext实例
		// ActionContext ctx = ActionContext.getContext();

		// 调用业务逻辑方法来处理登录请求
		int result = userService.validLogin(this.getUserId(), this.getPasswd());
		switch (result) {
		// 登录成功
		case LOGIN_SUCCESS:
			return SUCCESS;
			// 登录失败，用户名不存在
		case LOGIN_UNEXIST_USER:
			this.addActionError(getText("LOGIN_UNEXIST_USERNAME"));
			return ERROR;
			// 登录失败，密码错误
		case LOGIN_WRONG_PASSWORD:
			this.addActionError(getText("LOGIN_WRONG_PASSWORD"));
			return ERROR;
			// 缺省登录失败
		default:
			return ERROR;
		}
	}
}
